package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompAlipayMchntManageService;
import cc.rengu.igas.bomp.facade.request.*;
import cc.rengu.igas.bomp.facade.response.*;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by zhangxuran on 2020/04/02
 */
public class BompAlipayMchntManageServiceImpl implements BompAlipayMchntManageService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<AlipayActivityApplyQueryResponse> alipayActivityApplyQuery(AlipayActivityApplyQueryRequest alipayActivityApplyQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayActivityApplyQueryResponse> result = new Result<>();
        AlipayActivityApplyQueryResponse alipayActivityApplyQueryResponse = new AlipayActivityApplyQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(alipayActivityApplyQueryRequest, AlipayActivityApplyQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlipayActivityApplyQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("支付宝特殊费率申请商户列表查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                alipayActivityApplyQueryResponse.setRespCode(((BizException) e).getCode());
                alipayActivityApplyQueryResponse.setRespDesc(e.getMessage());
            } else {
                alipayActivityApplyQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                alipayActivityApplyQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AlipayActivityApplyResponse> alipayActivityApply(AlipayActivityApplyRequest alipayActivityApplyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayActivityApplyResponse> result = new Result<>();
        AlipayActivityApplyResponse alipayActivityApplyResponse = new AlipayActivityApplyResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(alipayActivityApplyRequest, AlipayActivityApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlipayActivityApplyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("支付宝特殊费率申请处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                alipayActivityApplyResponse.setRespCode(((BizException) e).getCode());
                alipayActivityApplyResponse.setRespDesc(e.getMessage());
            } else {
                alipayActivityApplyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                alipayActivityApplyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AlipayActivityApplyViewResponse> alipayActivityApplyView(AlipayActivityApplyViewRequest alipayActivityApplyViewRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayActivityApplyViewResponse> result = new Result<>();
        AlipayActivityApplyViewResponse alipayActivityApplyViewResponse = new AlipayActivityApplyViewResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(alipayActivityApplyViewRequest, AlipayActivityApplyViewResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlipayActivityApplyViewResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("支付宝特殊费率申请查看查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                alipayActivityApplyViewResponse.setRespCode(((BizException) e).getCode());
                alipayActivityApplyViewResponse.setRespDesc(e.getMessage());
            } else {
                alipayActivityApplyViewResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                alipayActivityApplyViewResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliPayMchntQueryResponse> aliPayMchntQuery(AliPayMchntQueryRequest aliPayMchntQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliPayMchntQueryResponse> result = new Result<>();
        AliPayMchntQueryResponse aliPayMchntQueryResponse = new AliPayMchntQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(aliPayMchntQueryRequest, AliPayMchntQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliPayMchntQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("支付宝入驻信息查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                aliPayMchntQueryResponse.setRespCode(((BizException) e).getCode());
                aliPayMchntQueryResponse.setRespDesc(e.getMessage());
            } else {
                aliPayMchntQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliPayMchntQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    @Override
    public Result<AliPayRiskMchntQueryResponse> aliPayRiskMchntQuery(AliPayRiskMchntQueryRequest aliPayRiskMchntQueryRequest) {
        {
            DubboService dubboService = new DubboServiceImpl();
            Result<AliPayRiskMchntQueryResponse> result = new Result<>();
            AliPayRiskMchntQueryResponse aliPayRiskMchntQueryResponse = new AliPayRiskMchntQueryResponse();
            try {
                DubboResult dubboResult = dubboService.callBizService(aliPayRiskMchntQueryRequest, AliPayRiskMchntQueryResponse.class);
                result.setSuccess(dubboResult.isSuccess());
                result.setResult((AliPayRiskMchntQueryResponse) dubboResult.getResult());
            } catch (Exception e) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
                rglog.error("支付宝风险商户违规查询处理失败:<{}>", byteArrayOutputStream.toString());
                result.setSuccess(false);
                if (e instanceof BizException) {
                    aliPayRiskMchntQueryResponse.setRespCode(((BizException) e).getCode());
                    aliPayRiskMchntQueryResponse.setRespDesc(e.getMessage());
                } else {
                    aliPayRiskMchntQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                    aliPayRiskMchntQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                }
            }
            return result;
        }
    }

    @Override
    public Result<AlipayActivityApplyConfirmResponse> alipayActivityApplyConfirm(AlipayActivityApplyConfirmRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayActivityApplyConfirmResponse> result = new Result<>();
        AlipayActivityApplyConfirmResponse alipayActivityApplyConfirmResponse = new AlipayActivityApplyConfirmResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, AlipayActivityApplyConfirmResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlipayActivityApplyConfirmResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("支付宝特殊费率申请确认处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                alipayActivityApplyConfirmResponse.setRespCode(((BizException) e).getCode());
                alipayActivityApplyConfirmResponse.setRespDesc(e.getMessage());
            } else {
                alipayActivityApplyConfirmResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                alipayActivityApplyConfirmResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
